package com.emit.security;


import cn.hutool.core.util.ObjectUtil;
import com.emit.dao.*;
import com.emit.domain.*;
import com.emit.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * security user service
 *
 * @author zhangchao
 * @version 1.0.0
 * @date 2021-02-28
 */
@Slf4j
@Service(value = "myUserDetailsService")
public class MyUserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    SysPermissionMapper sysPermissionMapper;
    @Autowired
    SysRoleMapper sysRoleMapper;
    @Autowired
    SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    SysRolePermissionMapper rolePermissionMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (StringUtils.isEmpty(username)) {
            throw new BizException("用户名不能为空");
        }
        //查询当前用户
        SysUserExample sysUserExample =new SysUserExample();
        sysUserExample.createCriteria().andUsernameEqualTo(username);
        List<SysUser> sysUsers = userMapper.selectByExample(sysUserExample);
        SysUser sysUser =null;
        if (!sysUsers.isEmpty()){
            sysUser = sysUsers.get(0);
        }
        //如果当前用户不存在则返回空
        if (sysUser == null) {
            return null;
        }
        //查询对应角色
        SysUserRoleExample userRoleExample =new SysUserRoleExample();
        userRoleExample.createCriteria().andUserIdEqualTo(sysUser.getId());
        List<SysUserRole> byRoleId = sysUserRoleMapper.selectByExample(userRoleExample);
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        // 声明用户授权
        List<SysPermission> resourceVOList = new ArrayList<>();
        if (byRoleId != null && byRoleId.size() != 0) {
            byRoleId.forEach(userRole -> {
                //根据角色id查询具体权限
                SysRolePermissionExample rolePermissionExample=new SysRolePermissionExample();
                rolePermissionExample.createCriteria().andRoleIdEqualTo(userRole.getId());
                List<SysRolePermission> sysRoleResources = rolePermissionMapper.selectByExample(rolePermissionExample);
                List<Long> sysResourceList = new ArrayList<>();
                if (!ObjectUtil.isEmpty(sysRoleResources)) {
                    sysRoleResources.forEach(sysRoleResource -> {
                        sysResourceList.add(sysRoleResource.getPermissionId());
                    });
                }
                sysResourceList.forEach(resourceId ->{
                    //添加角色对应资源
                    SysPermission sysPermission = sysPermissionMapper.selectByPrimaryKey(resourceId);
                    GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(sysRoleMapper.selectByPrimaryKey(userRole.getId()).getRoleName());
                    resourceVOList.add(sysPermission);
                    grantedAuthorities.add(grantedAuthority);
                });
            });
        }
        User myUser = new MyUserView(sysUser.getUsername(), sysUser.getPassword(), sysUser.getId(), resourceVOList,  grantedAuthorities);
        return myUser;
    }
}
